﻿package com.alstudio.utils.formater.age;

import com.alstudio.app.ALLocalEnv;
import com.loovee.imaohu.R;

public class ConstellationUtil {
	private static String[] constellations = ALLocalEnv.getLocalEnv().getResources().getStringArray(R.array.constellations);

	public enum Constellation {
		Capricorn(1, constellations[0]), Aquarius(2, constellations[1]), Pisces(3, constellations[2]), Aries(4,
				constellations[3]), Taurus(5, constellations[4]), Gemini(6, constellations[5]), Cancer(7, 
				constellations[6]), Leo(8, constellations[7]), Virgo(9, constellations[8]), Libra(10, constellations[9]), 
				Scorpio(11, constellations[10]), Sagittarius(12, constellations[11]);

		private Constellation(int code, String name) {
			this.code = code;
			this.name = name;
		}

		private int code;
		private String name;

		public int getCode() {
			return this.code;
		}

		public String getName() {
			return this.name;
		}
	}

	public static final Constellation[] constellationArr = {
			Constellation.Aquarius, Constellation.Pisces, Constellation.Aries,
			Constellation.Taurus, Constellation.Gemini, Constellation.Cancer,
			Constellation.Leo, Constellation.Virgo, Constellation.Libra,
			Constellation.Scorpio, Constellation.Sagittarius,
			Constellation.Capricorn };

	public static final int[] constellationEdgeDay = { 20, 19, 21, 21, 21, 22,
			23, 23, 23, 23, 22, 22 };

	public static String calculateConstellation(String birthday) {
		if (birthday == null || birthday.trim().length() == 0)
			throw new IllegalArgumentException("the birthday can not be null");
		String[] birthdayElements = birthday.split("-");
		if (birthdayElements.length != 3)
			throw new IllegalArgumentException(
					"the birthday form is not invalid");
		int month = Integer.parseInt(birthdayElements[1]);
		int day = Integer.parseInt(birthdayElements[2]);
		if (month == 0 || day == 0 || month > 12)
			return "";
		month = day < constellationEdgeDay[month - 1] ? month - 1 : month;
		return month > 0 ? constellationArr[month - 1].getName()
				: constellationArr[11].getName();
	}
}
